import java.io.*;
import java.net.*;
 
class Server
{
    public static void main(String args[])throws Exception
    {
    	ServerSocket server = new ServerSocket(5555);
    	
    	try{    
        	System.out.println("Server running...");         	  
 
        	Socket sk = server.accept();
       
        	System.out.println("Server accepted client");
        	InputStream input = sk.getInputStream();
        	BufferedReader inReader = new BufferedReader(new InputStreamReader(sk.getInputStream()));
        	BufferedWriter outReader = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));
        
        	String filename = inReader.readLine();
        
        	if ( !filename.equals("") ){          
        		outReader.write("READY\n");
        		outReader.flush();
        	} 
        	
        	FileOutputStream wr = new FileOutputStream(new File(filename));
 
        	byte[] buffer = new byte[sk.getReceiveBufferSize()]; 
        	int bytesReceived = 0; 
        	while((bytesReceived = input.read(buffer))>0)
        	{           
        		wr.write(buffer,0,bytesReceived);
        	}
        	wr.close(); 
        	System.out.println("Transfer complite");        	 
        }catch(IOException e){
        	System.err.println("IO Error");
        	try{        		
        		server.close(); 
        	}catch (NullPointerException e1){
            System.err.println("Socket closing error");
        	}        	
        }          
    }
}